package com.application.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.application.appenum.CONFIG_KEY;
import com.application.appenum.CTYPE;
import com.application.appenum.NET_TYPE;
import com.application.common.BaseContext;
import com.application.iface.IConfigure;
import com.application.iface.IMessage;
import com.application.iface.IPacket;






public class BaseConfig extends ConfigUtil implements IConfigure{
	
	protected static final String DEFAULT_CONFIG_CLASS = "default.config.class" ;
	protected static String configClassName="BaseConfig";
	/**
	 * ESB_TYPE C-client S-server
	 */
	private String esbType;
	
	private String sysCode;
	
	/**
	 * 杩滅缃戠粶
	 */
	private List<ConfigNetwork> remoteNetList = null;
	
	/**
	 * 鏈�濂界殑杩滅
	 */
	private ConfigNetwork bestRemote = null;
	
	/**
	 * 璇锋眰瓒呮椂
	 */
	private int remoteTimeout;
	
	/**
	 * 鏈湴缃戠粶
	 */
	private List<ConfigNetwork> localNetList = null;	
	
	/**
	 * 鍝嶅簲瓒呮椂
	 */
	private int localTimeout;	
		
	/**
	 * 	 蹇冭烦鏃堕棿
	 * */
	private int deteTiem;
	
	/**
	 * 服务配置
	 */
	private Map<String,ConfigService> serviceList = null;
	
	
	private static BaseConfig me=null;
	protected BaseConfig(){
		
	}
	public static BaseConfig getInstance(){						
		if(me == null){
			configClassName = getStringByKey(DEFAULT_CONFIG_CLASS);
			Map<String,BaseConfig> allPacket = BaseContext.context.getBeansOfType(BaseConfig.class);
			if(allPacket == null){
				me = new BaseConfig();
				return me;
			}
			
			for(BaseConfig p :allPacket.values()){
				if(p.getClass().getName().equals(configClassName)){
					me = p;
					return me;
				}
			}			
			me = new BaseConfig();
			return me;
		}
		return me;
	}
	// 获取网络配置
	public List<ConfigNetwork> LoadNetConfig(CTYPE flg){
		
		List<ConfigNetwork> lst= new ArrayList<ConfigNetwork>();
		
		String ips = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_IP.getValue());
		String ports = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_PORT.getValue());
		String tiems = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_TOUT.getValue());
		String netTypes = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_NTYPE.getValue());
		String pcNames = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_PCLSNAME.getValue());
		String mcNmaes = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_MCLSNAME.getValue());
		String hcNames = ConfigUtil.getStringByKey(CONFIG_KEY.REMOTE_HCLSNAME.getValue());

		if(CTYPE.LOCALE.equals(flg)){
			 ips = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_IP.getValue());
			 ports = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_PORT.getValue());
			 tiems = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_TOUT.getValue());
			 netTypes = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_NTYPE.getValue());
			 pcNames = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_PCLSNAME.getValue());
			 mcNmaes = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_MCLSNAME.getValue());
			 hcNames = ConfigUtil.getStringByKey(CONFIG_KEY.LOCALE_HCLSNAME.getValue());
		}else{
			
		}
		
		String[] ipss = ips.split(";");
		String[] portss = ports.split(";");
		String[] netTypess = netTypes.split(";");
		
		String[] pcNamess = null;
		if(null !=pcNames)
			pcNamess = pcNames.split(";");
		
		String[] mcNmaess = null;
		if(null !=mcNmaes)
			mcNmaess = mcNmaes.split(";");
		
		String[] hcNmaess = null;
		if(null !=hcNames)
			hcNmaess = hcNames.split(";");
		
		for(int i= 0; i< ipss.length;i++){
			ConfigNetwork o = new ConfigNetwork();
			o.setIp(ipss[i]);
			o.setPort(Integer.parseInt(portss[i]));
			o.setTimeout(Integer.parseInt(tiems));
			o.setNetType(NET_TYPE.SERVER.getKey().equals(netTypess[i])?NET_TYPE.SERVER:NET_TYPE.CLIENT);
			if(null !=pcNamess){
				if(null != pcNamess[i]){
					o.setPacketClazName(pcNamess[i]);
				}				
			}
			if(null !=mcNmaess){
				if(null != mcNmaess[i]){
					o.setMsgClazName(mcNmaess[i]);
				}				
			}
			if(null !=hcNmaess){
				if(null != hcNmaess[i]){
					o.setHandleClazName(hcNmaess[i]);
				}				
			}
			if(0 == i && CTYPE.REMOTE.equals(flg)){
				bestRemote = o;
			}
			lst.add(o);
		}
		return lst;
	}	
	// 中断线程
	public void killThread(CTYPE flg){
		
		List<ConfigNetwork> lst = null;
		if(CTYPE.LOCALE.equals(flg)){
			lst = this.localNetList;
		}else{
			lst = this.remoteNetList;
		}
		for (ConfigNetwork n:lst){
			n.interrupt();
		}
	}
	// 获取配置
	public void LoadConfig(){
		deteTiem = Integer.parseInt(ConfigUtil.getStringByKey(CONFIG_KEY.DETECT_TIME.getValue()));
		sysCode = ConfigUtil.getStringByKey(CONFIG_KEY.SYS_CODE.getValue());
		esbType = ConfigUtil.getStringByKey(CONFIG_KEY.ESB_TYPE.getValue());

		if(null == remoteNetList){
			remoteNetList = new ArrayList<ConfigNetwork>();
		}else{
			remoteNetList.clear();
		}	
		remoteNetList = LoadNetConfig(CTYPE.REMOTE);
		
		if(null == localNetList){
			localNetList = new ArrayList<ConfigNetwork>();
		}else{
			killThread(CTYPE.LOCALE);
			localNetList.clear();
		}	
		localNetList = LoadNetConfig(CTYPE.LOCALE);		
		
		LoadMoreConfig(); // 子类加载更多配置
	}
	
	//
	public void LoadMoreConfig() {
		
	}
	
	public String getEsbType() {
		return esbType;
	}

	public void setEsbType(String esbType) {
		this.esbType = esbType;
	}

	public String getSysCode() {
		return sysCode;
	}

	public void setSysCode(String sysCode) {
		this.sysCode = sysCode;
	}

	public List<ConfigNetwork> getRemoteNetList() {
		return remoteNetList;
	}

	public void setRemoteNetList(List<ConfigNetwork> remoteNetList) {
		this.remoteNetList = remoteNetList;
	}

	public ConfigNetwork getBestRemote() {
		return bestRemote;
	}

	public void setBestRemote(ConfigNetwork bestRemote) {
		this.bestRemote = bestRemote;
	}

	public int getRemoteTimeout() {
		return remoteTimeout;
	}

	public void setRemoteTimeout(int remoteTimeout) {
		this.remoteTimeout = remoteTimeout;
	}

	public List<ConfigNetwork> getLocalNetList() {
		return localNetList;
	}

	public void setLocalNetList(List<ConfigNetwork> localNetList) {
		this.localNetList = localNetList;
	}

	public int getLocalTimeout() {
		return localTimeout;
	}

	public void setLocalTimeout(int localTimeout) {
		this.localTimeout = localTimeout;
	}

	public int getDeteTiem() {
		return deteTiem;
	}

	public void setDeteTiem(int deteTiem) {
		this.deteTiem = deteTiem;
	}
	/**
	 *  启动服务 不同端口接收不同报文
	 */
	public void runAllServer(){
		for(ConfigNetwork cn:localNetList){
			cn.start();
		}
	}
	public Map<String, ConfigService> getServiceList() {
		return serviceList;
	}
	public void setServiceList(Map<String, ConfigService> serviceList) {
		this.serviceList = serviceList;
	}
	
	public void doService(IPacket pack,IMessage msg) throws Exception{
		ConfigService cs = serviceList.get(msg.getSerivceId());
		if(cs == null){
			throw new Exception();
		}
		cs.doStart(pack,msg);		
	}
}
